package com.yanqu.road.server.gameplayer.module.activity;

import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.entity.activity.ActivityDisperseGoldResult;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eDecorationType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wpj
 * @Date 2021/1/23
 */
public class ActivityTrystModule extends GeneralModule {

    public ActivityTrystModule(GamePlayer player) {
        super(player);
    }

    /**
     * 苗寨送财
     * @param activityInfo
     * @param manifesto
     * @return
     */
    public ActivityDisperseGoldResult disperseGold(ActivityInfo activityInfo, String manifesto) {
        ActivityDisperseGoldResult result = new ActivityDisperseGoldResult();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            result.setRet(GameErrorCode.E_TRYST_ACTIVITY_NO_IN_TIME);
            return result;
        }
        int itemId = Integer.parseInt(StringUtils.stringToStringList(activityInfo.getParam4(), ";").get(2));
        if (itemId == 0 || GoodsMgr.getGoodsById(itemId) == null) {
            result.setRet(GameErrorCode.E_TRYST_ACTIVITY_DISPERSE_GOLD_ITEM_CONFIG_ERROR);
            return result;
        }
        //送财消耗
        Property consume = new Property(itemId, BigInteger.ONE);
        if (!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.Activity, eLogMoneyType.TrystBaoZhuDisperseGold)) {
            result.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
            return result;
        }

        List<Integer> decorationIdList = player.getModule(DecorationModule.class).getDecorationIdList(eDecorationType.HonoraryTitle.getValue());
        ActivityDisperseGoldInfo goldInfo = new ActivityDisperseGoldInfo();
        goldInfo.setActivityId(activityInfo.getActivityId());
        goldInfo.setType(activityInfo.getType());
        goldInfo.setUserId(player.getUserId());
        goldInfo.setManifesto(manifesto);
        goldInfo.setBeginTime(System.currentTimeMillis());
        goldInfo.setHonoraryTitles(StringUtils.listToString(decorationIdList, ","));
        goldInfo.setUserBaseInfo(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId()));

        List<Long> serverList = new ArrayList<>();
        serverList.add(GameServer.getInstance().getServerId());
        goldInfo.setServerList(serverList);
        result.setGoldInfo(goldInfo);
        return result;

    }

    /**
     * 领取善财奖励
     * @param activityInfo
     * @param earnSpeed
     * @return
     */
    public Property getDisperseGoldReward(ActivityInfo activityInfo, BigInteger earnSpeed) {
        int fuDaiId = Integer.valueOf(StringUtils.stringToStringList(activityInfo.getParam4(), ";").get(3));
        String disperseGoldParam = StringUtils.stringToStringList(activityInfo.getParam4(), ";").get(4);
        List<Integer> params = StringUtils.stringToIntegerList(disperseGoldParam, "\\|");
        RandomHelper r = new RandomHelper();
        BigInteger silver = earnSpeed.multiply(BigInteger.valueOf(r.next(params.get(0), params.get(1))));

        int gold = r.next(params.get(2), params.get(3));

        Property property = new Property();
        property.addProperty(GameConfig.GAME_MONEY_SILVER, silver);
        property.addProperty(fuDaiId, BigInteger.valueOf(gold));
        return property;
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

}
